<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('model_model','model');
	}
	
	function index(){

		$this->_data['list_model'] = $this->cm->get_all($this->tbl->model);

		$this->load->view('model/index',$this->_data);
	}
	
	function add(){
		$this->load->view('model/add',$this->_data);
	}
	
	function edit(){
		$id = $this->p['id'];

		$r = $this->cm->get_by_id($this->tbl->model,$id);
		if(empty($r)) die;

		$this->_data['r'] = $r;

		$this->load->view('model/add',$this->_data);
	}
	
	function save_do(){

		if($this->cm->in($this->tbl->model,array('mark'=>$this->p['mark'],'id <>'=>(int)$this->p['id']))) ejd('fail','已有相同标识的模型存在！');			

		//不能添加相同标识的数据
		if(empty($this->p['id'])){
			//创建模型数据表
			$this->model->new_tbl($this->p['mark']);
		}

		$this->cm->save($this->tbl->model,$this->p);

		ejd('succ-reload');
	}
	
	function del_do(){

		$id = $this->p['id'];

		$r_model = $this->cm->get_by_id($this->tbl->model,$id);

		//删除数据表
		if($this->model->del_tbl($r_model->mark)){
			$this->cm->delete($this->tbl->model,array('id'=>$id));
		}

		ejd('succ-reload','操作成功！');

	}

	function view_attr(){
		$id = $this->p['id'];

		$r = $this->cm->get_by_id($this->tbl->model,$id);
		if(empty($r)) die;
		$this->_data['r_model'] = $r;

		$list_attr = (array)unserialize($r->attr);
		foreach ($list_attr as $k => $v) {
			if(empty($v)) unset($list_attr[$k]);
		}
		$this->_data['attr'] = $list_attr;

		$this->load->view('model/attr',$this->_data);
	}
	
	function attr_add(){
		$id = $this->p['id'];
		$r = $this->cm->get_by_id($this->tbl->model,$id);
		if(empty($r)) die;

		$this->_data['r_model'] = $r;
		$this->_data['opt_attr_type'] = $this->attr_type();

		$this->load->view('model/attr_add',$this->_data);
	}
	
	function attr_add_do(){
		
		$id = $this->p['id'];
		$r = $this->cm->get_by_id($this->tbl->model,$id);
		if(empty($r)) die;
		unset($this->p['id']);
		unset($this->p['c']);
		unset($this->p['m']);
		unset($this->p['tmp']);

		$list_attr = (array)unserialize($r->attr);
		foreach ($list_attr as $k => $v) {
			if($this->p['name'] == $v['name']) ejd('fail','属性已存在！');
		}

		$list_attr[] = $this->p;

		$list_attr = array_filter($list_attr);

		$r->attr = serialize($list_attr);

		if($this->model->tbl_add_col($r->mark,$this->p['name'],$this->p['type'],$this->p['value'],$this->p['default'],$this->p['allow_null'])){
			$this->cm->save($this->tbl->model,$r);		
		}

		ejd('succ-reload','操作成功！');

	}
	
	function attr_ord_do(){
		
		$id = $this->p['id'];
		$r = $this->cm->get_by_id($this->tbl->model,$id);
		if(empty($r)) die;

		$list_attr = (array)unserialize($r->attr);

		$arr_ord = (array)$this->p['ord'];
		$list_new_attr = array();
		foreach ($arr_ord as $k => $v) {
			$list_new_attr[$v] = $list_attr[$v];
		}
		
		$r->attr = serialize($list_new_attr);

		$this->cm->save($this->tbl->model,$r);

		ejd('succ');

	}
	
	function attr_edit(){

		$id = $this->p['id'];
		$r = $this->cm->get_by_id($this->tbl->model,$id);
		if(empty($r)) die;
		$i = $this->p['i'];
		$list_attr = (array)unserialize($r->attr);

		$r_attr = $list_attr[$i];

		$this->_data['r_model'] = $r;

		if(!empty($this->p['type'])){
			$r_attr['type'] = $this->p['type'];
		}

		$this->_data['r_attr'] = $r_attr;

		$this->_data['opt_attr_type'] = $this->attr_type();
		$this->_data['i'] = $i;

		$this->load->view('model/attr_edit',$this->_data);

	}
		
						
	function attr_edit_do(){
		
		$id = $this->p['id'];
		$i = $this->p['i'];
		$r = $this->cm->get_by_id($this->tbl->model,$id);
		if(empty($r)) die;

		unset($this->p['id']);
		unset($this->p['c']);
		unset($this->p['m']);
		unset($this->p['tmp']);
		unset($this->p['i']);

		$list_attr = (array)unserialize($r->attr);

		
		foreach ($list_attr as $k => $v) {
			if($k!=$i && $this->p['name'] == $v['name']) ejd('fail','属性已存在！');
		}
		$o_col_name = $list_attr[intval($i)]['name'];

		$list_attr[intval($i)] = $this->p;

		$r->attr = serialize($list_attr);

		if($this->model->tbl_edit_col($r->mark,$o_col_name,$this->p['name'],$this->p['type'],$this->p['value'],$this->p['default'],$this->p['allow_null'])){
			$this->cm->save($this->tbl->model,$r);		
			ejd('succ-reload');
		}else{
			ejd('fail','更新失败！');
		}


	}

	function attr_del(){
		
		$id = $this->p['id'];
		$i = $this->p['i'];
		$r = $this->cm->get_by_id($this->tbl->model,$id);
		if(empty($r)) die;

		unset($this->p['id']);
		unset($this->p['c']);
		unset($this->p['m']);
		unset($this->p['tmp']);
		unset($this->p['i']);

		$list_attr = (array)unserialize($r->attr);
		$r_attr = $list_attr[$i];
		unset($list_attr[$i]);

		$r->attr = serialize($list_attr);

		if($this->model->tbl_del_col($r->mark,$r_attr['name'])){
			$this->cm->save($this->tbl->model,$r);
		}

		ejd('succ-reload');

	}

	private function attr_type(){
		return array(
			'int'=>'整数',
			'money'=>'钱￥',
			'float'=>'小数',
			'text'=>'文本框',
			'textarea'=>'文本域',
			'html'=>'富文本',
			'image'=>'图片',
			'images'=>'图片集',
			'select'=>'下拉单选',
			'radio'=>'单选',
			'checkbox'=>'多选',
			'time'=>'时间',
			'foreign'=>'关联'
		);
	}

	function set_mid(){

		$_SESSION['mid'] = $this->p['mid'];
		ejd('succ-reload');
	}


}

